<?php

namespace app\admin\controller;

use app\admin\model\Customer;
use app\admin\model\Dep_customer;
use think\Controller;
use think\Request;
use app\admin\controller\Base;

class Customers extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = function($query){
            $query->field('password',true);
        };
        $list = Customer::all($where);

        return view('customer/index', ['data'=>$list]);
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $cus = Customer::get($id)->getData();
        $dep_cus = Dep_customer::get($id)->getData();
        $customer = array_merge($cus,$dep_cus);
        //var_dump($dep_cus);die();
        return view('customer/details',['customer'=>$customer]);
    }


    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = input('put.');
        $customer = Customer::get($id);
        $customer->status = $data['status'];
        $result = $customer->save();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '修改成功';
        }else{
            $data['status'] = false;
            $data['message'] = '修改失败';
        }
        return json($data);
    }

    public function editCustomer(){
        $info = input('post.');
        $id = $info['id'];

        $arr['name'] = $info['name'];
        $arr['sex'] = intval($info['sex']);
        $file = request()->file('head');
        if ($file){
            $res = $file->move(ROOT_PATH  . 'public/static/upload/head');
            if($res){
                $arr['headimg'] = $res->getSaveName();
            }else{
                // 上传失败获取错误信息
                return  $file->getError();
            }
        }

        $cus_dep = new Dep_customer();
        $result = $cus_dep->save($arr,['id'=>$id]);

        if ($result > 0){
            $this->success('修改成功',url('admin/index/index'));
        }else{
            echo "<script>history.back();</script>";
        }
    }

}
